package com.platinadesign.axa.view
{
	import com.platinadesign.axa.event.PageTurningEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Point;
	import flash.sensors.Accelerometer;
	import flashx.textLayout.elements.InlineGraphicElement;

	public class TurningPages extends Sprite
	{
		public static const ALL_PAGES_TURNED : String = "TurningPages::ALL_PAGES_TURNED";

		private static const TOTAL_DURATION : int = 2;

		private var _bottomLayer : Sprite;

		private var _leftBottom : Shape;

		private var _rightBottom : Shape;

		private var _topLayer : Sprite;

		private var _subPages : Vector.<FlipingPage>;

		private var _toLeft : Boolean;

		private var _overPages : Vector.<Page>;

		public function TurningPages()
		{
			this.addEventListener( Event.ADDED_TO_STAGE, addedToStageHandler );
			initLayers();
		}

		public function startTurning( direction : String, overPages : Vector.<Page> ) : void
		{
			_toLeft = ( direction == PageTurningEvent.TURN_TO_LEFT );
			_overPages = overPages;

			clearLastPages();
			//当翻书时，当前页的前半页和目标页的后半页是静止的，需要先绘制出来，然后在之上放置动画页
			drawBottom();
			//然后就可以放置动画页了
			createFlipingPages();
			//按照顺序添加到场景中，并且开始翻页序列
			layoutAndInitPages();
		}

		protected function addedToStageHandler( event : Event ) : void
		{
			this.removeEventListener( Event.ADDED_TO_STAGE, addedToStageHandler )

			var pp : PerspectiveProjection = new PerspectiveProjection();
			pp.projectionCenter = new Point( 0, this.stage.stageHeight * 0.5 );
			pp.fieldOfView = 10;
			this.transform.perspectiveProjection = pp;
		}

		protected function flipingFinishedHandler( event : Event ) : void
		{
			const finishedPage : FlipingPage = event.currentTarget as FlipingPage;
			const index : int = _subPages.indexOf( finishedPage );

			if( index == _subPages.length - 1 )
			{
				this.dispatchEvent( new Event( ALL_PAGES_TURNED, true ));
			}
			else
			{
				_topLayer.addChild(_subPages[ index + 1 ]);
				//_topLayer.swapChildren( _subPages[ index ], _subPages[ index + 1 ]);
				_subPages[ index + 1 ].startFliping();
			}
			finishedPage.removeEventListener( FlipingPage.ONE_PAGE_TURNED, flipingFinishedHandler );
		}

		private function layoutAndInitPages() : void
		{
			for( var i : int = _subPages.length - 1; i >= 0; i-- )
			{
				const f : FlipingPage = _subPages[ i ];
				f.addEventListener( FlipingPage.ONE_PAGE_TURNED, flipingFinishedHandler );
				_topLayer.addChild( f );
			}
			_subPages[ 0 ].startFliping();
		}

		private function clearLastPages() : void
		{
			while( _topLayer.numChildren > 0 )
			{
				_topLayer.removeChildAt( 0 );
			}

			if( _subPages && _subPages.length > 0 )
			{
				for each( var f : FlipingPage in _subPages )
				{
					f.depose();
				}
			}
			_subPages = new Vector.<FlipingPage>();
		}

		private function createFlipingPages() : void
		{
			//两页书，翻一页，三页书翻两页.
			for( var i : int = 0; i < _overPages.length - 1; i++ )
			{
				const currentPage : Page = _overPages[ i ];
				const nextPage : Page = _overPages[ i + 1 ];

				var firstSide : BitmapData;

			/*	if( i == 0 )
				{
					firstSide = _toLeft ? currentPage.currentRightSide : currentPage.currentLeftSide;
				}
				else
				{*/
					firstSide = _toLeft ? currentPage.rightSide : currentPage.leftSide;
				//}
				var secondSide : BitmapData = _toLeft ? nextPage.leftSide : nextPage.rightSide;

				const flipingPage : FlipingPage = new FlipingPage( firstSide, secondSide, TOTAL_DURATION / _overPages.length, _toLeft );
				_subPages.push( flipingPage );
			}
		}

		private function initLayers() : void
		{
			_leftBottom = new Shape();
			_rightBottom = new Shape();
			this.addChild( _bottomLayer = new Sprite());
			_bottomLayer.addChild( _leftBottom );
			_bottomLayer.addChild( _rightBottom );
			_topLayer = new Sprite();
			this.addChild(_topLayer);
		}

		private function drawSide( canvas : Shape, bmd : BitmapData, transparent : Boolean = false ) : void
		{
			canvas.graphics.clear();

			if( transparent )
			{
				trace( 'clear' )
				return;
			}
			canvas.graphics.beginBitmapFill( bmd );
			canvas.graphics.drawRect( 0, 0, bmd.width, bmd.height );
			canvas.graphics.endFill();
		}

		private function drawBottom() : void
		{
			const firstPage : Page = _overPages[ _toLeft ? 0 : ( _overPages.length - 1 )];
			const lastPage : Page = _overPages[ _toLeft ? ( _overPages.length - 1 ) : 0 ];
			const leftSideOfFirstPage : BitmapData = firstPage.leftSide;
			const rightSideOfLastPage : BitmapData = lastPage.rightSide;
			drawSide( _leftBottom, leftSideOfFirstPage, firstPage.transparentHalf );
			drawSide( _rightBottom, rightSideOfLastPage, lastPage.transparentHalf );

			_leftBottom.x = -_leftBottom.width;
			_rightBottom.x = 0;
		}
	}
}